package com.jeremy;

import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.Scanner;

public class SpeedTest {

    private static ChromeDriver driver;
    private static ArrayList<HashMap<String, String>> speedTestInfo = new ArrayList<>();


    public static void main(String[] args) throws InterruptedException, IOException {
        Scanner scanner = new Scanner(System.in);
        System.out.print("请输入循环次数：");
        int loopTimes = scanner.nextInt();
        //获取当前时间
        Date date = new Date();
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String time = df.format(date);
        //获取浏览器对象
        driver = new ChromeDriver();
        //窗口最大化
        driver.manage().window().maximize();
        //打开测速网站
        driver.get("https://www.speedtest.cn/");
        Thread.sleep(3000);
        //获取测速按钮元素并点击
        WebElement p_ele1 = findEleByXpath("/html/body/main/section/div[1]/div[2]/div[3]/div[1]/p");
        p_ele1.click();
        Thread.sleep(45000);
        //循环测速操作
        for (int i = 1; i < loopTimes; i++) {
            System.out.println("第" + i + "次");
            //记录当前测速结果
            saveSpeedInfo();
            //开始下次测速
            try {
                WebElement close_ad_window1 = findEleByXpath("/html/body/main/div[1]/div[9]/div/img[2]");
                close_ad_window1.click();
                System.out.println("关闭第一个广告");
            } catch (Exception e) {
                System.out.println("找不到第一个广告关闭按钮");
            }

            try {
                WebElement close_ad_window2 = findEleByXpath("/html/body/main/div[2]/div/img");
                close_ad_window2.click();
                System.out.println("关闭第二个广告");
            } catch (Exception e) {
                System.out.println("找不到第二个广告关闭按钮");
            }
            try {
                nextTest();
            } catch (Exception e) {
                e.printStackTrace();
                driver.close();
            }
        }
        System.out.println("第" + loopTimes + "次");
        //保存最后一次测速结果
        saveSpeedInfo();
        System.out.println("speedTestInfo = " + speedTestInfo);
        File file = new File("speed_test_result.txt");
        FileWriter fileWriter = new FileWriter(file, true);
        BufferedWriter writer = new BufferedWriter(fileWriter);
        writer.write(time);
        writer.write(" ");
        writer.write(speedTestInfo.toString());
        writer.newLine();
        writer.close();
        //关闭浏览器
        driver.close();
    }

    /*
     * 测速完成进行下次测速
     * */
    private static void nextTest() throws InterruptedException {
        try {
            WebElement p_ele2 = findEleByXpath("/html/body/main/section/div[1]/div[2]/div[3]/div[3]/div[1]/div[2]/p");
            p_ele2.click();
        } catch (Exception e) {
            e.printStackTrace();
        }
        Thread.sleep(45000);
    }

    /*
     * 根据xpath查找元素
     * */
    public static WebElement findEleByXpath(String path) {
        return driver.findElement(By.xpath(path));
    }

    /*
     * 保存当前页面上的测速结果
     * */
    public static void saveSpeedInfo() {
        WebElement download_speed_ele = findEleByXpath("/html/body/main/section/div[1]/div[2]/div[3]/div[3]/div[2]/div[1]/div[2]/a/dl/dd");
        String download_speed = download_speed_ele.getText();
        WebElement upload_speed_ele = findEleByXpath("/html/body/main/section/div[1]/div[2]/div[3]/div[3]/div[2]/div[1]/div[3]/a/dl/dd");
        String upload_speed = upload_speed_ele.getText();
        HashMap<String, String> TestInfo = new HashMap<>();
        TestInfo.put("download", download_speed);
        TestInfo.put("upload", upload_speed);
        speedTestInfo.add(TestInfo);
        System.out.println("TestInfo = " + TestInfo);
    }
}
